﻿using System;
using System.Windows;
using InnvoiceActual.Logica.GestorDB;
using InnvoiceActual.Logica.GestorObject.Articulo;
using System.Windows.Input;
using System.ComponentModel;
using InnvoiceActual.Logica.GestorObject;

namespace InnvoiceActual.Ws.Articulos.SubArticulos
{
    /// <summary>
    /// Interaction logic for AltaArticulo.xaml
    /// </summary>
    public partial class AltaSubArticulo : Window
    {

        #region Property

        private SubArticulo subArticulo = new SubArticulo();
        private ArticuloDB articuloDB = new ArticuloDB();
        private AlicuotaDB alicuotaDB = new AlicuotaDB();
        EmpresaCS empresa = EmpresaCS.Instance;

        #endregion

        #region Contructor

        public AltaSubArticulo()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            subArticulo.PropertyChanged += new PropertyChangedEventHandler(subArticulo_PropertyChanged);
            DataContext = subArticulo;
            textDescripcionSubArticulo.Focus();

        }

        #endregion

        #region Windows Methods

        private void btnGuardar_Click(object sender, RoutedEventArgs e)
        {
            GuardarElemento();
        }

        private void btnCancelar_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            categoriaComboBox.Items.Clear();
            categoriaComboBox.ItemsSource = articuloDB.ObtenerCategoriasDeArticulosSegunEstado(1);

            CategoriaArticulo categoria = (CategoriaArticulo)categoriaComboBox.SelectedItem;
            int codigoCategoria = categoria.Codigo;

            articuloComboBox.ItemsSource = articuloDB.ObtenerArticulos(codigoCategoria);

            alicuotaComboBox.Items.Clear();
            alicuotaComboBox.ItemsSource = alicuotaDB.GetAlicuotas(1);
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key.ToString().ToUpper())
            {
                case "F1":
                    GuardarElemento();
                    break;
                case "ESCAPE":
                    this.Close();
                    break;
                default:
                    return;
             }
        }

        private void categoriaComboBox_DropDownClosed(object sender, EventArgs e)
        {
            CategoriaArticulo categoria = (CategoriaArticulo)categoriaComboBox.SelectedItem;
            int codigoCategoria = categoria.Codigo;
            
            articuloComboBox.ItemsSource = articuloDB.ObtenerArticulos(codigoCategoria);
        }

        private void subArticulo_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            btnGuardar.IsEnabled = subArticulo.IsValid();
        }

        #endregion

        #region Auxiliar Methods

        private void GuardarElemento()
        {
            subArticulo.CodigoCategoria = Convert.ToInt16(categoriaComboBox.SelectedValue);
            subArticulo.CodigoArticulo = Convert.ToInt16(articuloComboBox.SelectedValue);
            subArticulo.Codigo_Alicuota = Convert.ToInt16(alicuotaComboBox.SelectedValue);

            articuloDB.AddSubArticulo(subArticulo);
            this.Close();
        }

        #endregion
    }
}
